package logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.shwread.http.util.Logger;

import java.util.ArrayList;
import java.util.List;

import logic.bean.ShoppingCart;
import logic.db.DBHelper;
import logic.table.ShoppingCartTable;
import logic.table.ShoppingCartTable.ShoppingCartColumns;

/**
 * 购物车  数据访问类
 * @author 刘宾  
 * @date 2015年4月7日 下午8:34:02
 */
public class ShoppingCartDao
{
	private Context              context;
	private static ShoppingCartDao       dao;
	private DBHelper             dbHelper;
	
	private ShoppingCartDao(Context context) {
		this.context = context;
		dbHelper = DBHelper.getInstance(context);
	}
	
	public static ShoppingCartDao getInstance(Context context) {
		dao = new ShoppingCartDao(context);
		return dao;
	}
	/**
	 * 根据id查询购物车信息
	 * @param account
	 * @param id
	 * @return
	 */
	public ShoppingCart query(String account,long id){
		ShoppingCart bean = null;
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.COLUMN_ID + " = ? ";
		String[] selectionArgs = new String[] { account,String.valueOf(id) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(ShoppingCartTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(ShoppingCart.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return bean;
	}
	/**
	 * 检查是否存在相同商品
	 */
	public ShoppingCart exists(ShoppingCart cart){
		ShoppingCart bean = null;
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.GOODS_ID + " = ? and " +ShoppingCartColumns.SKU_ID + " =? and "+ShoppingCartColumns.ACTIVITY_ID +"=? ";
		String[] selectionArgs = new String[] { cart.account,String.valueOf(cart.goodsId),String.valueOf(cart.skuId),String.valueOf(cart.activityId) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(ShoppingCartTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				c.moveToFirst();
				bean = dbHelper.getObject(ShoppingCart.class, c);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return bean;
	}
	/**
	 * 根据账号查询购物车信息列表
	 * @param account
	 * @return
	 */
	public List<ShoppingCart> queryByAccount(String account){
		List<ShoppingCart> list = null;
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.ORDERS_STATUS + " = ? ";
		String[] selectionArgs = new String[] { account,String.valueOf(ShoppingCart.ORDERSTATUS_NORMAL) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(ShoppingCartTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				list = new ArrayList<ShoppingCart>();
				while(c.moveToNext()){
					list.add(dbHelper.getObject(ShoppingCart.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return list;
	}
	/**
	 *
	 * 查询订单提交失败的信息列表
	 * @param account
	 * @return
	 */
	public List<ShoppingCart> queryByFail(String account){//TODO
		List<ShoppingCart> list = null;
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.ORDERS_STATUS + " = ? ";
		String[] selectionArgs = new String[] { account,String.valueOf(ShoppingCart.ORDERSTATUS_FAIL) };
		Cursor c = dbHelper.getSqliteDatabaseR().query(ShoppingCartTable.table, null, selection, selectionArgs, null, null, null);
		try
		{
			if (null != c)
			{
				list = new ArrayList<ShoppingCart>();
				while(c.moveToNext()){
					list.add(dbHelper.getObject(ShoppingCart.class, c));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return list;
	}
	/**
	 * 更新购物车数据
	 * @param cart 购物车对象
	 * @return
	 */
	public int update(ShoppingCart cart){
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.COLUMN_ID + " = ? ";
		String[] selectionArgs = new String[] { cart.account,String.valueOf(cart.id) };
		
		ContentValues values = null;
        try
        {
	        values = dbHelper.getContentValues(DBHelper.TYPE_UPDATE, cart);
        }
        catch (Exception e)
        {
	        e.printStackTrace();
	        Logger.e("更新购物车数据失败！", e);
	        return 0;
        }
		return dbHelper.getSqliteDatabaseR().update(ShoppingCartTable.table, values , selection, selectionArgs);
	}
	/**
	 * 查询购物车里的总数量
	 * @param account
	 * @return
	 */
	public long queryCount(String account){
		long count = 0;
		String sql = "select count(" + ShoppingCartColumns.COLUMN_ID + ") from " + ShoppingCartTable.table + " where " + ShoppingCartColumns.ACCOUNT + " = ? ";
		String[] selectionArgs = new String[] { account };
		Cursor c = dbHelper.getSqliteDatabaseR().rawQuery(sql, selectionArgs);
		try
		{
			if (null != c)
			{
				c.moveToNext();
				count = c.getLong(0);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
		}
		return count;
	}
	
	/**
	 * 添加购物车
	 * @param list
	 */
	public long add(ShoppingCart bean){
		ShoppingCart temp = exists(bean);
		if(temp==null){
			try
			{
				ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_INSERT, bean);
				return dbHelper.getSqliteDatabaseW().insert(ShoppingCartTable.table, null, values);
			}
			catch (Exception e)
			{
				Logger.e("添加购物车失败,ShoppingCart:"+bean.toString(),e);
				e.printStackTrace();
			}
		}else{
			temp.count += bean.count;
			return update(temp);
		}
        return -1;
	}
	
	/**
	 * 根据id删除购物车数据
	 * @return
	 */
	public int deleteById(String account,long id){
		String selection = ShoppingCartColumns.ACCOUNT + " = ? and " +ShoppingCartColumns.COLUMN_ID + " = ? ";
		String[] selectionArgs = new String[] { account,String.valueOf(id) };
		return dbHelper.getSqliteDatabaseW().delete(ShoppingCartTable.table, selection, selectionArgs);
	}
	/**
	 * 根据账号删除全部购物车数据
	 * @return
	 */
	public int deleteAll(String account){
		String selection = ShoppingCartColumns.ACCOUNT + " = ? ";
		String[] selectionArgs = new String[] { account };
		return dbHelper.getSqliteDatabaseW().delete(ShoppingCartTable.table, selection, selectionArgs);
	}
}
